package com.taxshare.seller.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.response.beans.Response;
import com.taxshare.seller.entity.QuotaEntity;
import com.taxshare.seller.mapper.QuotaMapper;
import com.taxshare.seller.util.LqHttpUtil;
import com.taxshare.tools.util.UUIDUitl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: QuotaService
 * @Description:
 * @author: lpp
 * @date: 2024/10/21 10:37
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class QuotaService extends ServiceImpl<QuotaMapper, QuotaEntity> {

    @Value("${lqpt.ptbh}")
    private String ptbh;
    @Value("${lqpt.url}")
    private String url;

    @Value("${empower.url}")
    private String empowerUrl;

    @Autowired
    QuotaMapper quotaMapper;
    @Autowired(required = false)
    LqHttpUtil lqHttpUtil;
    /**
     * 查询发票号码段
     *
     * @param quotaEntity
     * @return
     */
    public Response query(PageRequest<QuotaEntity> quotaEntity) {
        QueryWrapper<QuotaEntity> queryWrapper = new QueryWrapper();
        QuotaEntity quotaInfo = quotaEntity.getInfo();
        if (!StrUtil.hasBlank(quotaInfo.getNsrsbh())) {
            queryWrapper.eq("nsrsbh", quotaInfo.getNsrsbh());
        }
        if (!StrUtil.hasBlank(quotaInfo.getSq())) {
            queryWrapper.eq("sq", quotaInfo.getSq());
        }
        queryWrapper.orderByDesc("create_time");
        IPage<QuotaEntity> result = PageHelp.siamplePage(this, queryWrapper, quotaEntity.getPage(), QuotaEntity.class);
        return Response.success(result);
    }

    /**
     * 查询额度
     * @param param
     * @return
     */
    public Map<String,Object> CXSXED(Map<String, Object> param){
        String nsrsbh = String.valueOf(param.get("nsrsbh"));
        Map<String,Object> resultMap = new HashMap();
        log.info("========查询授信额度========开始======");
        JSONObject reqJson = new JSONObject();
        reqJson.put("nsrsbh",nsrsbh);//（销售方）统一社会信用代码/纳税人识别号/身份证件号码
        String reqStr = reqJson.toJSONString();
        log.info("========入参======>>>"+reqStr);
        try{
            url = url+"202007/CXSXED";
            JSONObject result_json = lqHttpUtil.sendPostLq(reqStr,url);
            String returncode = result_json.getString("returncode");
            if("00".equals(returncode)) {
                String bysxed = result_json.getString("bysxed");
                String kysyed = result_json.getString("kysyed");
                String yxzed = result_json.getString("yxzed");
                String yxzwsyed = result_json.getString("yxzwsyed");
                String ztsxbz = result_json.getString("ztsxbz");
                String sq = result_json.getString("sq");
                QuotaEntity quota = new QuotaEntity();
                quota.setUuid(UUIDUitl.UUID32());
                quota.setNsrsbh(nsrsbh);
                quota.setBysxed(bysxed);
                quota.setKysyed(kysyed);
                quota.setYxzed(yxzed);
                quota.setYxzwsyed(yxzwsyed);
                quota.setZtsxbz(ztsxbz);
                quota.setSq(sq);
                Map<String, Object> quotaMap = new HashMap<>();
                quotaMap.put("sq", sq);
                quotaMap.put("nsrsbh", nsrsbh);
                quotaMapper.delQuotaBysq(quotaMap);
                quotaMapper.insert(quota);
                resultMap.put("code", "0000");
                resultMap.put("msg", "成功");
            }else{
                resultMap.put("code", "0000");
                resultMap.put("msg", "成功");
            }
        }catch (Exception e){
            log.error("查询授信额度,异常：",e);
            resultMap.put("code","9999");
            resultMap.put("msg",e);
            e.printStackTrace();
        }
        log.info("========查询授信额度========结束======");
        return resultMap;
    }

    /**
     * 下载额度
     * @param param
     * @return
     */
    public Map<String,Object> XZTHSXED(Map<String, Object> param){
        String sqlx = String.valueOf(param.get("sqlx"));
        String nsrsbh = String.valueOf(param.get("nsrsbh"));
        String sqed = String.valueOf(param.get("sqed"));

        Map<String,Object> resultMap = new HashMap();
        log.info("========下载或退回授信额度========开始======");
        JSONObject reqJson = new JSONObject();
        reqJson.put("nsrsbh",nsrsbh);//（销售方）统一社会信用代码/纳税人识别号/身份证件号码
        reqJson.put("ptbh",ptbh);//平台编号
        reqJson.put("sqlx",sqlx);//申请类型 0：下载 1：退回
        reqJson.put("sqed",sqed);//申请额度
        reqJson.put("ywlsh",ptbh+ptbh+ UUIDUitl.UUID32());//流水号
        String reqStr = reqJson.toJSONString();
        log.info("========入参======>>>"+reqStr);
        try{
            url = url+"202007/XZTHSXED";
            JSONObject result_json = lqHttpUtil.sendPostLq(reqStr,url);
            String returncode = result_json.getString("returncode");
            if("00".equals(returncode)) {
                resultMap = CXSXED(param);
            }
        }catch (Exception e){
            log.error("下载或退回授信额度,异常：",e);
            resultMap.put("code","9999");
            resultMap.put("msg",e);
            e.printStackTrace();
        }
        log.info("========下载或退回授信额度========结束======");
        return resultMap;
    }



}


